angular.module('starter.services', [])

.service('shopData',['$http','$rootScope','$interval',function($http,$rootScope,$interval) {
  // Might use a resource here that returns a JSON array
 $rootScope.URL="http://www.hulupi.com/thinkphp/";//线上
//$rootScope.URL="http://192.168.0.103/tp/";//线下
  // Some fake testing data

   			this.shop=function(scope){//首页数据
   				$rootScope.logo=true;
	   			$http.get($rootScope.URL+'Home/Category/cate_index').success(function(data){
	   					scope.ify=data;
	   					$rootScope.logo=false;
	   			}).error(function(data){

   			});}
	   			this.shops=function(scope){//首页数据
	   				$rootScope.logo=true;
	   					$http.get($rootScope.URL+'Home/Category/region').success(function(data){
	   					scope.ifys=data;
	   					$rootScope.logo=false;
	   					
	   					
	   			}).error(function(data){
	   				console.log(data);
	   			})};
	   			this.number=function(cartId,num,uid){//数量加减
	   					$http.get($rootScope.URL+'Home/Cart/change_cart',{params:{
	   						cart_id:cartId,
	   						number:num,
	   						user_id:uid
	   						
	   					}})
	   					.success(function(data){
	   					// console.log(data);	
	   						num=0;
	   					}).error(function(data){
	   			})}	   		
	   			this.pro=function(ID,de){//分类 （一级二级三级）
	   				$rootScope.logo=true;
	   				$http({
						method:'get',
						url:$rootScope.URL+'Home/Category/cate',
					}).success(function(data){
						$rootScope.logo=false;

						$rootScope.aisDe=data;
						for (var key in $rootScope.aisDe){
							if(ID==$rootScope.aisDe[key].id){
								 $rootScope.arr =[];
								for(var obj in $rootScope.aisDe[key].data){
									$rootScope.arr.push($rootScope.aisDe[key].data[obj]);
								}

							}
							
						}
							$rootScope.Pro=$rootScope.arr;


					}).error(function(data){
					})}

					this.goods=function(ID){//详情页（综合）
						$rootScope.logo=true;
						$http.get($rootScope.URL+'Home/Goods/goods',{
							params:{
								cate_id:ID
							}
						}).success(function(data){
							console.log(data);
							$rootScope.logo=false;
							$rootScope.goods=data;
							$rootScope.id=ID;

						}).error(function(data){
							console.log(false);
						})
					}

						this.comm=function(ID){//详情页（综合）
							$rootScope.logo==true;
						$http.get($rootScope.URL+'Home/Goods/goods',{
							params:{
								cate_id:ID
							}
						}).success(function(data){
								$rootScope.logo=false;
							$rootScope.goods=data;
						}).error(function(data){
							console.log(false);
						})
					}	
					this.sales=function(ID){//详情页（销量）
						$rootScope.logo=true;
						$http.get($rootScope.URL+'Home/Goods/goods',{
							params:{
								cate_id:ID,
								sales:'sales'
							}
						}).success(function(data){
							$rootScope.logo=false;
							$rootScope.goods=data;

						}).error(function(data){
							console.log(false);
						})
					}
					this.price=function(ID){//详情页（价格）
						$rootScope.logo=true;
						$http.get($rootScope.URL+'Home/Goods/goods',{
							params:{
								cate_id:ID,
								shop_price:'shop_price'
							}
						}).success(function(data){
							$rootScope.logo=false;
							$rootScope.goods=data;
						}).error(function(data){
							console.log(false);
						})
					}
					this.prod=function(ID,uid){//商品详情
						$rootScope.logo=true;
							$http.get($rootScope.URL+'Home/Goods/good',{
							params:{
								id:ID,
								uid,uid
							}
						}).success(function(data){
							$rootScope.logo=false;
							var arr=JSON.parse(localStorage.getItem("pro"));
		          			localStorage.setItem("pro",JSON.stringify(data));
						}).error(function(data){
							console.log(false);
						})
					}

					this.scree=function(ID){//筛选
							$http.get($rootScope.URL+'Home/Category/select',{
								params:{
									cate_id:ID
							 	}
							}).success(function(data){
								$rootScope.select=data;
										var arr=[];
									for(var i=0;i<$rootScope.select.region.length;i++){
										arr.push($rootScope.select.region[i].id);
							
									}
									$rootScope.num2=arr.join(',');
								var arr1=[];
									for(var i=0;i<$rootScope.select.brand.length;i++){
										arr1.push($rootScope.select.brand[i].id);	
									}
									$rootScope.num1=arr1.join(',');



							}).error(function(data){
								console.log(false)
							});
					}
				this.srceeData=function(ID,brand,add_id,low,hin){
				$rootScope.logo=true;//筛选传参(分类id，品牌id，地区id；低价，高价)
							$http.get($rootScope.URL+'Home/Goods/select',{
								params:{
									cate_id:ID,
									brand_id:brand==null?$rootScope.num1:brand,
									region_id:add_id==null?$rootScope.num2:add_id,
									min_price:low==''?0:low,
									max_price:hin==''?0:hin
								
							 	}
							}).success(function(data){
								$rootScope.logo=false;
								$rootScope.goods=data;
								console.log($rootScope.selec);
									 	$rootScope.nu.low='';
										$rootScope.nu.high='';
										brand='';
										add_id='';
										$rootScope.geAdd=null;
										$rootScope.geBrand=null;

							}).error(function(data){
								console.log(false)
							});
					
						};
						this.cha=function(uid,token,Pid,num){//加入到购物车(用户ID，token,商品id，数量);
							 console.log(Pid);
							$http.get($rootScope.URL+'Home/Cart/cart',{params:{
								uid:uid,
								good_id:Pid,
								number:num,
								token:token

							}}).success(function(data){
								 console.log(data);


							}).error(function(data){
								console.log(false)
							});
						}
						this.shopCha=function(uid){//购物车
							$rootScope.logo=true;
							$http.get($rootScope.URL+'Home/Cart/cart',{params:{
								uid:uid
							}}).success(function(data){
								$rootScope.logo=false;
								$rootScope.address_id=data.address_id;
								 $rootScope.chas=data.info;
							}).error(function(data){
								console.log(false)
							});
						}

						this.del=function(uid,proId){//删除功能
							$http.get($rootScope.URL+'Home/Cart/delete_cart',{params:{
								uid:uid,
								good_id:proId
							}}).success(function(data){
								 $rootScope.chas=data;
							}).error(function(data){
								console.log(false)
							});

						}
					this.check=function(cart,uid,address_id){//结算功能
						$http.get($rootScope.URL+'Home/Cart/check',{params:{
								cart_id:cart,
								uid:uid,
								address_id:address_id
							}}).success(function(data){
								console.log(data);
								$rootScope.defaultAddress=data.default_address;
								$rootScope.proGoods=data.goods;
								$rootScope.can_credit=data.can_credit;
								$rootScope.all_price=data.all_price;
								$rootScope.can_cred=$rootScope.can_credit/100;
								$rootScope.postage=data.postage;
								$rootScope.totals=data.total;
								$rootScope.defaulId=$rootScope.defaultAddress.id;
									

							}).error(function(data){
								console.log(false)
							});
					}
					this.submit=function(addres,cart,uid,jfId,totals,postage,all_price){//提交订单
							$http.get($rootScope.URL+'Home/Cart/create_order',{params:{
								address_id:addres,
								cart_id:cart,
								uid:uid,
								credit:jfId,
								all_price:totals,
								postage:postage,
								total:all_price
								
							}}).success(function(data){
									console.log(data);
							}).error(function(data){
								console.log(false)
							});
					}

			this.addRss=function(uid){//地址管理
		
					$http.get($rootScope.URL+'Home/Address/address',
			   				{params:{
			   				uid:uid
			   			}
			   		}).success(function(data){
			   		
			   					$rootScope.addres=data.address;
			   					$rootScope.idd=data.default_address.id;
			   			}).error(function(data){

		   			})

			}
			this.area=function(id){//选择地址
					$http.get($rootScope.URL+'Home/Address/now_address',
			   				{params:{
			   				id:id
			   			}
			   		}).success(function(data){
			   					$rootScope.defaultAddress=data;
			   					$rootScope.defaultId=$rootScope.defaultAddress.id;
			   				

			   			}).error(function(data){
		   			})

			}

		this.editor=function(id){//编辑地址
				$http.get($rootScope.URL+'Home/Address/now_address',
	   				{params:{
	   					id:id
	   			}
	   		}).success(function(data){
	   			if(data==null){
	   				return;
	   			}else{
					$rootScope.editorId=data.id;
					$rootScope.editorAdd=data.address;
					$rootScope.addRess={user:data.consignee,tel:data.tel,serial:data.zipcode,detail:data.detail_address};
				}
	   			}).error(function(data){

   			})
		}
		this.amendId=function(id,userId,user,tel,zipcod,addr,deti){//编辑修改地址(传过来的id，用户id，用户，电话，邮箱，地址，详情地址。)
				$http.get($rootScope.URL+'Home/Address/edit_address',
	   				{params:{
	   					id:id,
	   					user_id:userId,
		   				address:addr,
		   				detail_address:deti,
		   				zipcode:zipcod,
		   				consignee:user,
		   				tel:tel
	   			}
	   		}).success(function(data){	
	   			}).error(function(data){
	   				console.log(false);

   			})
		}
		this.amendDel=function(id){//删除地址(传过来的id)
				$http.get($rootScope.URL+'Home/Address/delete_address',{params:{id:id}
	   		}).success(function(data){
	   			console.log(data);
	   				
	   			}).error(function(data){
	   				console.log(false);

   			})
		}

		this.setDefault=function(id,uid){//设为默认地址(传过来的id)
				$http.get($rootScope.URL+'Home/Address/default_address',{params:{id:uid,address_id:id}
	   		}).success(function(data){
	   			console.log(data);
	   			}).error(function(data){
	   				console.log(false);

   			})
		}


	this.addNew=function(uid,user,tel,zipcode,detail,address){//添加地址(用户id，电话，手机，右边，详细地址，地址)
	   			$http.get($rootScope.URL+'Home/Cart/address',
	   				{params:{
	   				user_id:uid,
	   				address:address,
	   				detail_address:detail,
	   				zipcode:zipcode,
	   				consignee:user,
	   				tel:tel
	   			}
	   		}).success(function(data){
	   				
	   			}).error(function(data){

   			})

	}



  this.time=function(scope,time,s,text){
  		scope.hinT=time;
		scope.eg=text;
				scope.hintT=$interval(function(){
					scope.hinT--;
						if(scope.hinT==0){
							$interval.cancel(scope.hintT);
								scope.hint=false;
							return;
						}else {
							scope.hint=true;
						}
				},s);

				return;
  } 		
  

  
		this.begin=function(num1,num2,scope,state){//登录页
			$http({
				method:'get',
				url:$rootScope.URL+'Home/UserCenter/login',
				params:{name:num1,password:num2}
			}).success(function(data){
				console.log(data);
				if(data.code==-1){
				scope.hinT=3;
				scope.eg=data.msg;
				scope.hintT=$interval(function(){
					scope.hinT--;
						if(scope.hinT==0){
							$interval.cancel(scope.hintT);
								scope.hint=false;
							return;
						}else {
							scope.hint=true;
						}
				},1000);

	
						return;
				}else if(data.code==-2){
				scope.hinT=3;
				scope.eg=data.msg;
				scope.hintT=$interval(function(){
					scope.hinT--;
						if(scope.hinT==0){
							$interval.cancel(scope.hintT);
								scope.hint=false;
							return;
						}else {
							scope.hint=true;
						}
				},1000);
					return;
				}
				else if(data.info.address==null){
					//$state.go('address');
					// return ;
				}
			

					state.go('tab.shop');

					var user=JSON.parse(localStorage.getItem("User"));
			            if(!user){
			            	user=[];
			            }
		            var obj={"userN":data.info.account,'uid':data.info.id,'token':data.token}
		            user.push(obj);
		            localStorage.setItem("User",JSON.stringify(user));

				}).error(function(){
					console.log('false');
				})
	


	}
		this.fpList=function(uid){//用户浏览记录(用户id)
		$http.get($rootScope.URL+'Home/Goods/footprint',
			{params:{
				user_id:uid
			}
			}).success(function(data){
			$rootScope.fpdata = data;
		}).error(function(data){

		})

	}
	this.cList=function(uid){//用户收藏记录(用户id)
		$http.get($rootScope.URL+'Home/Goods/collection',
			{params:{
				user_id:uid
			}
			}).success(function(data){
			//console.log(data);
			$rootScope.cdata = data;
		}).error(function(data){
		})
	}
	this.cancelc=function(cid,uid){//取消收藏
		$http.get($rootScope.URL+'Home/Goods/cancelc',
			{params:{
				user_id:uid,
				cid:cid
			}
			}).success(function(data){
			console.log(data);
		}).error(function(data){
		})
	}

	this.distribution=function(id,scope){//获得分销的个人中心的数据
		$http.get($rootScope.URL+'Home/Partner/index',
			{params:{
				uid:id
			}
			}).success(function(data){
				console.log(data);
				scope.num = data.num;
				scope.detail_count = data.detail_cash.length;
				scope.commission = data.user_info.commission;
				scope.draw_commission = data.user_info.draw_commission;
				scope.recommend = data.recommend;
		}).error(function(data){

		})
	}
	this.detail_cash=function(id,scope){//获得分销的佣金明细
		$http.get($rootScope.URL+'Home/Partner/index',
			{params:{
				uid:id
			}
			}).success(function(data){

			scope.detail_cash = data.detail_cash;
			console.log(scope.detail_cash);
		}).error(function(data){

		})
	}
	this.offline_user=function(id,scope){//获得分销的下线
		$http.get($rootScope.URL+'Home/Partner/index',
			{params:{
				uid:id
			}
			}).success(function(data){

			scope.offline_user = data.offline_user;
			console.log(scope.detail_cash);
		}).error(function(data){

		})
	}

}]);
